﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Media;

namespace DataLayer
{
    public class ErrorTypesHelper : SystemFileHelper
    {
         static ErrorTypesHelper instance;
        static public ErrorTypesHelper GetInstance()
        {
            if (instance == null)
                instance = new ErrorTypesHelper();
            return instance;
        }

        private ErrorTypesHelper()
        {
            pathFile = "./SystemFile/Error.xml";
            this.listObject = LoadAll();
        }

        public override List<SystemFile> LoadAll()
        {            
            List<SystemFile> listError = new List<SystemFile>();
            XmlDocument document = new XmlDocument();
            XmlTextReader reader = new XmlTextReader(pathFile);
            reader.Read();
            // load reader
            document.Load(reader);

            for (int i = 0; i < document.FirstChild.ChildNodes.Count; i++)
            {
                ErrorType err = new ErrorType();
                XmlNode currentNode = document.FirstChild.ChildNodes[i];
                err.Description=currentNode.SelectSingleNode("Description").InnerText;
                BrushConverter conv = new BrushConverter();
                err.Color = conv.ConvertFromString(currentNode.SelectSingleNode("Color").InnerText) as SolidColorBrush;
                err.Id = int.Parse(currentNode.SelectSingleNode("ID").InnerText);
                err.Suggestion = currentNode.SelectSingleNode("Suggestion").InnerText;
                err.Boundary = int.Parse(currentNode.SelectSingleNode("Boundary").InnerText);
                err.Name = currentNode.SelectSingleNode("Name").InnerText;
                listError.Add(err);
            }
            reader.Close();
            return listError;
        }

        public ErrorType GetErrorTypeByName(string name)
        {
            ErrorType rs = null;
            for (int i = 0; i < this.listObject.Count; i++)
            {
                if (((ErrorType)this.listObject[i]).Name.Equals(name))
                {
                    rs = (ErrorType)this.listObject[i];
                }
            }
            return rs;
        }
        public ErrorType GetErrorTypeByID(int Id)
        {
            ErrorType rs = null;
            for (int i = 0; i < this.listObject.Count; i++)
            {
                if (((ErrorType)this.listObject[i]).Id.Equals(Id))
                {
                    rs = (ErrorType)this.listObject[i];
                }
            }
            return rs;
        }
        public override void Update()
        {
            base.Update();
        }
    }
}
